package cn.growthgate.fgo.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import cn.growthgate.fgo.common.base.BaseServiceImpl;
import cn.growthgate.fgo.entity.MyItem;
import cn.growthgate.fgo.mapper.MyItemMapper;
import cn.growthgate.fgo.service.IMyItemService;

/**
 * <p>
 * 我的素材 服务实现类
 * </p>
 *
 * @author GrowthGate
 * @since 2019-06-25
 */
@Service
public class MyItemServiceImpl extends BaseServiceImpl<MyItemMapper, MyItem> implements IMyItemService {

	@Override
	public boolean save(MyItem myItem) {
		// 查询数据库是否原有数据
		MyItem item = selectOne(newWrapper().eq("itemId", myItem.getItemId()));
		if (item == null) {
			item = myItem;
			// 防止恶意赋id覆盖数据库数据
			item.setId(null);
		} else {
			item.setNumber(myItem.getNumber());
		}
		return insertOrUpdate(item);
	}

	@Override
	public Map<Long, Integer> getAll() {
		// 此方法构建的map中value不能为null
		// return selectAll().stream().collect(Collectors.toMap(MyItem::getItemId, MyItem::getNumber));
		Map<Long, Integer> map = new HashMap<>();
		selectAll().forEach(myItem -> map.put(myItem.getItemId(), myItem.getNumber()));
		return map;
	}

}
